package com.example.navigation.fragment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.navigation.R;

public class DetailFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_detail, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Button button = getView().findViewById(R.id.button2);
        //从上个页面传递的bundle获取数据 并且打印
        /*Bundle arguments = getArguments();
        String userName = arguments.getString("userName");
        Log.d("guaimiao" , "userName:" + userName);*/

        //通过safe args插件从上个页面传递的bundle中获取数据 并且打印
        HomeFragmentArgs homeFragmentArgs = HomeFragmentArgs.fromBundle(getArguments());
        String userName = homeFragmentArgs.getUserName();
        int age = homeFragmentArgs.getAge();
        Log.d("guaimiao", "userName:" + userName + "age:" + age);
        button.setOnClickListener(view -> {
            NavController navController = Navigation.findNavController(view);
            navController.navigate(R.id.action_detailFragment_to_homeFragment);
        });
    }
}